<template>
  <div>
    <!--    查询功能-->
    <div style="margin-bottom: 5px">
      <!--      重置按钮-->
      <el-button type="success" @click="resetParam">重置</el-button>
      <!--      新增按钮-->
      <el-button type="primary" style="margin-left: 5px" @click="add">新增</el-button>
    </div>
    <!--    列表展示-->
    <el-table :data="records" :header-cell-style="{ background: '#D3D3D3FF', color: '#555555' }">
      <el-table-column prop="id" label="id" width="160"> </el-table-column>
      <el-table-column label="图片" width="150" header-align="center">
        <template slot-scope="scope">
          <el-image style="width: 150px; height: 150px" :src="scope.row.image" fit="contain"
            :preview-src-list="[scope.row.image]"></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="姓名" width="80">
      </el-table-column>
      <el-table-column prop="byClass" label="班级" width="130" header-align="center">
      </el-table-column>
      <el-table-column prop="tagFirst" label="第一条标签" width="100">
      </el-table-column>
      <el-table-column prop="tagSecond" label="第二条标签" width="100">
      </el-table-column>
      <el-table-column prop="description" label="个人简介" width="120">
      </el-table-column>
      <el-table-column prop="wxNumber" label="微信号" width="120"> </el-table-column>
      <el-table-column prop="gmtCreate" label="创建时间" width="140">
      </el-table-column>
      <el-table-column prop="operate" label="操作">
        <template slot-scope="scope">
          <el-button size="small" type="success" @click="mod(scope.row)">编辑</el-button>
          <el-popconfirm confirm-button-text="确定" cancel-button-text="取消" icon="el-icon-info" icon-color="red"
            title="这是一段内容确定删除吗？" @confirm="del(scope.row.id)" style="margin-left: 5px">
            <el-button slot="reference" size="small" type="danger">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="报名管理信息" :visible.sync="centerDialogVisible" width="40%" center :before-close="handleClose">
      <!--      协会成员管理信息表单-->
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="图片" prop="id">
          <el-upload class="avatar-uploader" :action="actionUrl" :show-file-list="false"
            :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload" name="file" :data="uploadData"
            :headers="headers">
            <img v-if="form.image" :src="form.image" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件
            </div>
          </el-upload>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-col :span="15">
            <el-input v-model="form.name"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="班级" prop="byClass">
          <el-col :span="15">
            <el-input v-model="form.byClass"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="第一标签" prop="tagFirst">
          <el-col :span="15">
            <el-input type="textarea" v-model="form.tagFirst"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="第二标签" prop="tagSecond">
          <el-col :span="15">
            <el-input type="textarea" v-model="form.tagSecond"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="个人简介" prop="description">
          <el-col :span="15">
            <el-input type="textarea" v-model="form.description"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="微信号" prop="wxNumber">
          <el-col :span="15">
            <el-input v-model="form.wxNumber"></el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>
    <div style="margin-top: 10px; text-align: center">
      <div class="pagination-container">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page"
          :page-sizes="[5, 10, 20, 30]" :page-size="limit" layout="total, sizes, prev, pager, next, jumper"
          :total="total"></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { deleteMemberById } from "@/api/cmsService/AssociationMembers/deleteMemberById";
import { getMemberList } from "@/api/cmsService/AssociationMembers/getMemberList";
import { addOrUpdateMember } from "@/api/cmsService/AssociationMembers/addOrUpdateMember";

export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "AssociationMembers",
  data() {
    return {
      actionUrl: "http://106.52.100.34:8222/minioservice/minio-upload/upload",// 服务器上传API地址
      records: [],
      url: "",
      page: 1,
      limit: 10,
      total: 0,
      num: 1,
      member: {},
      centerDialogVisible: false,
      rules: {},
      form: {
        id: "",
        image: "",
        name: "",
        byClass: "",
        tagFirst: "",
        tagSecond: "",
        wxNumber: "",
        description: "",
        clubId: "",
      },
      uploadData: {
        bucketName: 'student' // 上传头像时的附加参数
      }
    };
  },
  methods: {
    handleSizeChange(newSize) {
      this.limit = newSize;
      this.page = 1;  // Reset to first page with new page size
      this.loadPost();
    },
    handleCurrentChange(newPage) {
      this.page = newPage;
      this.loadPost();
    },
    // 从 cookie 中获取 token
    getTokenFromCookie() {
      const cookies = document.cookie.split('; ');
      const token = cookies.find(row => row.startsWith('token='))?.split('=')[1];
      return token;
    },
    // 设置 headers
    setHeaders() {
      this.headers = {
        'token': this.getTokenFromCookie()
      };
    },
    //判断上传的是否为jpg、png格式
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isPNG = file.type === "image/png";

      if (!isJPG && !isPNG) {
        this.$message.error("上传头像图片只能是 JPG 格式 或者是 PNG 格式!");
      }
    },
    handleAvatarSuccess(res) {
      if (res.success) {
        this.form.image = res.data.url
      } else {
        this.$message.error('上传失败');
      }
    },
    //重置表单
    resetForm() {
      this.$refs.form.resetFields();
      this.form = {
        id: "",
        image: "",
        name: "",
        byClass: "",
        tagFirst: "",
        tagSecond: "",
        wxNumber: "",
        description: "",
        clubId: "",
      }
    },

    //新增按钮
    add() {
      this.centerDialogVisible = true;
      this.$nextTick(() => {
        this.resetForm();
      });
    },
    //修改
    mod(row) {
      this.centerDialogVisible = true;
      this.$nextTick(() => {
        //赋值到表单
        this.id = row.id;
        this.form.id = row.id;
        this.form.image = row.image;
        this.form.byClass = row.byClass;
        this.form.name = row.name;
        this.form.tagFirst = row.tagFirst;
        this.form.tagSecond = row.tagSecond;
        this.form.wxNumber = row.wxNumber;
        this.form.description = row.description;
        this.form.clubId = row.clubId;
      });
    },
    //删除
    del(id) {
      deleteMemberById(id).then((res) => {
        if (res.code === 20000) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.centerDialogVisible = false;
          this.loadPost();
        } else {
          this.$message({
            message: "删除失败",
            type: "error",
          });
        }
      });
    },
    //修改
    doMod() {
      addOrUpdateMember(this.form).then((res) => {
        if (res.code === 20000) {
          this.rec
          this.$message({
            message: "修改成功",
            type: "success",
          });
          this.centerDialogVisible = false;
          this.loadPost();
          this.resetForm();
        } else {
          this.$message({
            message: "修改失败",
            type: "error",
          });

        }
      });
    },
    //提交数据到后台
    save() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.id) {
            //如果有id则修改
            this.doMod();
          } else {
            //否则增加
            this.doSave();
          }
        } else {
          this.$message({
            message: "操作失败",
            type: "error",
          });
          return false;
        }
      });
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(() => {
          done();
        })
        .catch(() => { });
    },
    resetParam() {
      this.loadPost();
    },
    loadPost() {
      getMemberList(this.page, this.limit).then((res) => {
        if (res.code === 20000) {
          this.records = res.data.list.records;
          this.total = res.data.list.total;
        }
      });
    },
    doSave() {
      addOrUpdateMember(this.form).then((res) => {
        if (res.code === 20000) {
          this.$message({
            message: "增加成功",
            type: "success",
          });
          this.centerDialogVisible = false;
          this.loadPost();
          this.resetForm();
        } else {
          this.$message({
            message: "增加失败",
            type: "error",
          });
          console.log(res);

        }
      });
    },
  },
  beforeMount() {
    this.loadPost();
    this.setHeaders();
  },
};
</script>

<style scoped>
.pagination-wrapper {
  position: fixed;
  bottom: 0;
  width: 100%;
  padding: 10px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.avatar-uploader .avatar {
  width: 300px;
  height: 100px;
  display: block;
}

.avatar-uploader .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}

.rotating {
  position: absolute;
  width: 585px;
  height: 100%;
  right: 0;
  top: 0;
}
</style>
